﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="layui-fluid" style="padding:0 0px">
    <div class="layui-row layui-col-space5">
        <div class="layui-col-md4 layui-col-xs5">
            <div class="layui-card" style="text-align:left;overflow: auto;" id="toolbarDiv">
                <fieldset class="table-search-fieldset" style="border:1px solid #e6e6e6">
                    <legend>库区</legend>
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" id="item_keyword" name="item_keyword" autocomplete="off" class="layui-input" placeholder="">
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="item-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </fieldset>
                <ul id="demoTree" class="dtree" data-id="0"></ul>
            </div>
        </div>
        <div class="layui-col-md8 layui-col-xs7">
            <div class="layui-card" style="padding: 5px;">
                <fieldset class="table-search-fieldset" style="border:1px solid #e6e6e6">
                    <legend>库位</legend>
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <input type="text" id="txt_keyword" name="txt_keyword" autocomplete="off" class="layui-input" placeholder="">
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </fieldset>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container" id="toolbar">
                        <button id="NF-add" name="NF-add" authorize="yes" class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新建</button>
                        <button id="NF-edit" name="NF-edit" authorize="yes" class="layui-btn layui-btn-sm layui-btn-warm data-edit-btn layui-hide" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</button>
                        <button id="NF-delete" name="NF-delete" authorize="yes" class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn layui-hide" lay-event="delete"> <i class="layui-icon">&#xe640;</i>删除</button>
                        <button id="NF-details" name="NF-details" authorize="yes" class="layui-btn layui-btn-sm layui-btn-normal data-info-btn layui-hide" lay-event="details"> <i class="layui-icon">&#xe60b;</i>查看</button>
                        <button id="NF-print" name="NF-print" authorize class="layui-btn layui-btn-sm layui-hide" lay-event="print"> <i class="layui-icon">&#xe66d;</i>打印</button>
                        <button id="NF-banding" name="NF-banding" authorize class="layui-btn layui-btn-sm layui-hide" lay-event="banding"> <i class="fa fa-key"></i>绑定物料</button>
                        <button id="NF-import" authorize class="layui-btn layui-btn-sm data-import-btn" lay-event="import"> <i class="layui-icon">&#xe67c;</i>导入</button>
                    </div>
                </script>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['jquery', 'form', 'table', 'common', 'dtree', 'commonTable', 'optimizeSelectOption'], function () {
        var $ = layui.jquery,
            form = layui.form,
            commonTable = layui.commonTable,
            table = layui.table,
            dtree = layui.dtree,
            common = layui.common;
        //权限控制(js是值传递)
        toolbarDemo.innerHTML = common.authorizeButtonNew(toolbarDemo.innerHTML);
        // 初始化树
        var DemoTree = dtree.render({
            elem: "#demoTree",
            height: 'full-15',
            width: '135px',
            method: "GET",
            async: true,
            line: true,  // 显示树线
            scroll: "#toolbarDiv", // 绑定div元素
            initLevel: 1,
            url: "/MaterialManage/Area/GetTreeGridJson?v=" + new Date().Format("yyyy-MM-dd hh:mm:ss") // 使用url加载（可与data加载同时存在）
        });
        var treeid;
        // 绑定节点点击
        dtree.on("node('demoTree')", function (obj) {
            treeid = obj.param.nodeId;
            $("#txt_keyword").val('');
            $('button[lay-filter="data-search-btn"]').click();//按钮刷新
        });
        commonTable.rendertable({
            elem: '#currentTableId',
            id: 'currentTableId',
            data: [],
            search: false,
            limit: 9999,//每页数据 默认
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['count'] //自定义分页布局
                , first: false //不显示首页
                , last: false //不显示尾页
            },
            height: 'full-110',
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'F_LocationCode', title: '库位编号', minWidth: 140, sort: true, filter: true },
                { field: 'F_SortCode', title: '顺序', width: 140, sort: true, filter: true },
                {
                    field: 'F_LocationState', title: '使用', width: 100, sort: true, filter: true,
                    templet: function (d) {
                        if (d.F_LocationState == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>使用</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>空置</span>";
                        }
                    }
                },
                {
                    field: 'F_LocationType', title: '类型', width: 100, sort: true, filter: true,
                    templet: function (d) {
                        if (d.F_LocationType == 1) {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>临时</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-xs'>正常</span>";
                        }
                    }
                },
                {
                    field: 'F_EnabledMark', title: '状态', width: 100, sort: true, filter: true,
                    templet: function (d) {
                        if (d.F_EnabledMark == true) {
                            return "<span class='layui-btn layui-btn-normal layui-btn-xs'>有效</span>";
                        } else {
                            return "<span class='layui-btn layui-btn-warm layui-btn-xs'>无效</span>";
                        }
                    }
                },
                { field: 'F_MaterialName', title: '绑定物料', minWidth: 200, sort: true, filter: true },
                {
                    field: 'F_CreatorTime', title: '创建时间', width: 160, sort: true, filter: { type: 'date[yyyy/MM/dd HH:mm:ss]' }
                },
                { field: 'F_Description', title: '备注', minWidth: 120, sort: true, filter: true },
            ]]
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            common.ajax({
                url: "/MaterialManage/Location/GetGridJson",
                dataType: 'json',
                data: { itemId: treeid, keyword: data.field.txt_keyword },
                async: false,
                success: function (data) {
                    if (data.data) {
                        table.reload('currentTableId', {
                            data: data.data
                        });
                    }
                }
            });
            return false;
        });
        // 监听搜索操作
        form.on('submit(item-search-btn)', function (data) {
            // 调用内置函数搜索节点
            DemoTree.fuzzySearch(data.field.item_keyword);
            return false;
        });
        wcLoading.close();
        //行点击事件监听，控制按钮显示
        var oneList = ["NF-edit", "NF-details"];//选择1条显示
        var moreList = ["NF-delete", "NF-print", "NF-banding"];//选择多条显示
        commonTable.tableRowClick("checkbox", "currentTableFilter", "currentTableId", oneList, moreList);
        //toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var data = table.checkStatus('currentTableId').data;
            if (obj.event === 'add') {  // 监听添加操作
                common.modalOpen({
                    title: "添加界面",
                    url: "/MaterialManage/Location/Form?itemId=" + treeid,
                    width: "500px",
                    height: "500px",
                });
            }
            else if (obj.event === 'delete') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].F_Id);
                }
                common.deleteForm({
                    url: "/MaterialManage/Location/DeleteForm",
                    param: { keyValue: ids.join(',') },
                    success: function () {
                        common.reload('data-search-btn');
                    }
                });
            }
            else if (obj.event === 'edit') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalOpen({
                    title: "编辑界面",
                    url: "/MaterialManage/Location/Form?keyValue=" + data[0].F_Id,
                    width: "500px",
                    height: "500px",
                });
            }
            else if (obj.event === 'details') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                common.modalOpen({
                    title: "查看界面",
                    url: "/MaterialManage/Location/Details?keyValue=" + data[0].F_Id,
                    width: "500px",
                    height: "500px",
                    btn: []
                });
            }
            else if (obj.event === 'import') {
                common.modalOpen({
                    title: "导入界面",
                    url: "/MaterialManage/Location/Import",
                    width: "650px",
                    height: "650px",
                });
            }
            else if (obj.event === 'print') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                if (top.websocket == null || top.websocket.readyState == 3) {
                    common.modalMsg("打印服务未开启", "warning");
                    return false;
                }
                common.modalConfirm("注：您确定要打印选中这些数据吗？", function (r) {
                    if (r) {
                        var ids = [];
                        var json = {};
                        json.cmd = "print";
                        json.requestId = uuid();
                        json.data = {};
                        json.data.printIniInfo = {};
                        json.data.printIniInfo.filePath = window.document.location.origin + top.clients.printTemplates["二维码编号名称模板"];
                        json.data.printIniInfo.realName = "二维码编号名称模板";
                        json.data.printIniInfo.printType = 1;
                        json.data.printIniInfo.printName = "";
                        json.data.printIniInfo.landscape = true;
                        json.data.printIniInfo.paperSize = "";
                        json.data.printIniInfo.duplex = "";
                        json.data.data = [];
                        for (var i = 0; i < data.length; i++) {
                            var temp = {};
                            temp.barId = data[i].F_LocationCode;
                            temp.code = "库位编号";
                            temp.name = data[i].F_LocationCode;
                            json.data.data.push(temp);
                        }
                        top.sendWSPush(json);
                    }
                });
            }
            else if (obj.event === 'banding') {
                if (data.length == 0) {
                    common.modalMsg("未选中数据", "warning");
                    return false;
                }
                var ids = [];
                for (var i = 0; i < data.length; i++) {
                    ids.push(data[i].F_Id);
                }
                common.modalOpen({
                    title: "绑定界面",
                    url: "/MaterialManage/Location/AddForm?keyValue=" + ids.join(','),
                    width: "650px",
                    height: "650px",
                });
            }
            return false;
        });
        //toolrow监听事件
        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'delete') {
                common.deleteForm({
                    url: "/MaterialManage/Location/DeleteForm",
                    param: { keyValue: obj.data.F_Id },
                    success: function () {
                        obj.del();
                    }
                });
            }
            else if (obj.event === 'edit') {
                common.modalOpen({
                    title: "编辑界面",
                    url: "/MaterialManage/Location/Form?keyValue=" + obj.data.F_Id,
                    width: "500px",
                    height: "500px",
                });
            }
            else if (obj.event === 'details') {
                common.modalOpen({
                    title: "查看界面",
                    url: "/MaterialManage/Location/Details?keyValue=" + obj.data.F_Id,
                    width: "500px",
                    height: "500px",
                    btn: []
                });
            }
            return false;
        });
    });
</script>
